// pages/guiders/seetourist/seetourist.js
var Tool = require("../../../utils/ztool.js");
var tool = new Tool();
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDD: '',
    tourists: [],
    dateEnd:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    // console.log(options.endtime)
    this.setData({
      IDD: options.IDD,
      dateEnd: options.endtime
    })
    console.log(this.data.dateEnd)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getServiceData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getServiceData: function () {
    var that = this

    that.setData({
      tourists: [],
    })

    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)
    var tel = useinfo.sTel

    wx.request({
      url: 'https://www.lanshuicar.com/Server/GetCarsGuidersCusts.ashx',
      data: {
        IDD: that.data.IDD,
        UID: useinfo.Id,
        UType: useinfo.USerType,
        OType: 1,
        pagesize: 100,
        pageindex: 1
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {
          that.setData({
            tourists: that.data.tourists.concat(data.data),
          });

          console.log(that.data.tourists)
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  call: function (e) {
    console.log(e)
    var phoneNumber = e.currentTarget.dataset.text
    if (phoneNumber == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },

  locateDriver: function (e) {
    var that = this
    var open = e.currentTarget.dataset.open
    console.log(that.data.dateEnd)
    var time1 = util.formatTime1(new Date())
    console.log(time1)
    if (that.data.dateEnd < time1) {
      that.showError('订单已过期')
      return
    }
    if(open == '0'){
      that.showError('此乘客尚未开启定位功能')
      return
    }
    var driverID = e.currentTarget.id
    wx.navigateTo({
      url: '../../travelAgency/locate?IDD=' + that.data.IDD + '&DriverID=' + driverID + '&retype=5',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },
})